﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DeliveryOrderDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDeliveryOrderDetailDA deliveryOrderDetailDA = DataAccess.CreateDeliveryOrderDetailDA();
        private PubParasInfo pubParasInfo;

        public DeliveryOrderDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public DeliveryOrderDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public DeliveryOrderDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<DeliveryOrderDetailInfo> deliveryOrderDetailInfos = new List<DeliveryOrderDetailInfo>();
                DeliveryOrderDetailInfo deliveryOrderDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    deliveryOrderDetailInfo = deliveryOrderDetailDA.GetDeliveryOrderDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0], this.pubParasInfo);
                    deliveryOrderDetailInfos.Add(deliveryOrderDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (deliveryOrderDetailDA.DeleteDeliveryOrderDetail(dataBase, tran, mainID, subId, this.dataBaseID) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<DeliveryOrderDetailInfo>(dataBase, tran, deliveryOrderDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "DELIVERY_ORDER_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return deliveryOrderDetailDA.GetAllDeliveryOrderDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public string GetDeliveryOrderDetailStatus(DeliveryOrderDetailInfo deliveryOrderDetailInfo)
        {
            string Status = DeliveryOrderStatus.NEW;
            decimal AcceptQty = ((deliveryOrderDetailInfo.AcceptQty + deliveryOrderDetailInfo.LoseQty) + deliveryOrderDetailInfo.BreakageQty) + deliveryOrderDetailInfo.PkgDemagedQty;
            if (deliveryOrderDetailInfo != null)
            {
                if ((((deliveryOrderDetailInfo.Status == DeliveryOrderStatus.NEW) && (deliveryOrderDetailInfo.OriginalQty > deliveryOrderDetailInfo.LoadingQty)) && (deliveryOrderDetailInfo.LoadingQty > 0M)) && (AcceptQty == 0M))
                {
                    Status = DeliveryOrderStatus.PARTLOADED;
                }
                if (((deliveryOrderDetailInfo.OriginalQty == deliveryOrderDetailInfo.LoadingQty) && (deliveryOrderDetailInfo.LoadingQty > 0M)) && (AcceptQty == 0M))
                {
                    Status = DeliveryOrderStatus.LOADED;
                }
                if ((((deliveryOrderDetailInfo.Status == DeliveryOrderStatus.DISPATCHED) || (deliveryOrderDetailInfo.Status == DeliveryOrderStatus.PARTDISPATCHED)) && (deliveryOrderDetailInfo.OriginalQty == deliveryOrderDetailInfo.LoadingQty)) && (AcceptQty == 0M))
                {
                    Status = DeliveryOrderStatus.INTRANSIT;
                }
            }
            return Status;
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            DeliveryOrderDetailQueryEntity deliveryOrderDetailQuery = obj as DeliveryOrderDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(deliveryOrderDetailQuery.DeliveryOrderID))
            {
                sqlWhere.Add(" DELIVERY_ORDER_ID " + deliveryOrderDetailQuery.DeliveryOrderID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderDetailQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + deliveryOrderDetailQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderDetailQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + deliveryOrderDetailQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(deliveryOrderDetailQuery.Status))
            {
                sqlWhere.Add(" STATUS " + deliveryOrderDetailQuery.Status);
            }
            if (!string.IsNullOrEmpty(deliveryOrderDetailQuery.OriginalQty))
            {
                sqlWhere.Add(" ORIGINAL_QTY " + deliveryOrderDetailQuery.OriginalQty);
            }
            if (!string.IsNullOrEmpty(deliveryOrderDetailQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + deliveryOrderDetailQuery.IsActive);
            }
            return deliveryOrderDetailDA.GetDeliveryOrderDetailByQueryList(sqlWhere, deliveryOrderDetailQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DeliveryOrderDetailInfo GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return deliveryOrderDetailDA.GetDeliveryOrderDetailByID(mainID, subID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<DeliveryOrderDetailInfo> GetDetailInfoListByID(string mainID, string subID)
        {
            return deliveryOrderDetailDA.GetDeliveryOrderDetailListByID(mainID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<DeliveryOrderAndDispatchDataInfo> GetDetailInfoListByIDs(List<string> deliveryOrderIDs)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            List<DeliveryOrderAndDispatchDataInfo> deliveryOrderAndDispatchDataInfos = new List<DeliveryOrderAndDispatchDataInfo>();
            try
            {
                deliveryOrderAndDispatchDataInfos = deliveryOrderDetailDA.GetDeliveryOrderAndOrderDetailByIDs(dataBase, tran, deliveryOrderIDs, this.dataBaseID[0]);
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return deliveryOrderAndDispatchDataInfos;
        }

        public string InsertDetail(object obj)
        {
            DeliveryOrderDetailInfo deliveryOrderDetailInfo = obj as DeliveryOrderDetailInfo;
            string deliveryOrderID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (deliveryOrderDetailDA.CheckDeliveryOrderDetailIDUnique(dataBase, tran, deliveryOrderDetailInfo.DeliveryOrderID, deliveryOrderDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F8100_005", this.pubParasInfo.UserLanguage, new List<string> { deliveryOrderDetailInfo.DeliveryOrderID, deliveryOrderDetailInfo.LineID.ToString() });
                }
                if (deliveryOrderDetailInfo != null)
                {
                    deliveryOrderDetailInfo.UpdatedBy = deliveryOrderDetailInfo.CreatedBy;
                    deliveryOrderDetailInfo.CreatedDate = DateTime.Now;
                    deliveryOrderDetailInfo.UpdatedDate = DateTime.Now;
                    if (deliveryOrderDetailDA.InsertDeliveryOrderDetail(dataBase, tran, deliveryOrderDetailInfo, this.dataBaseID) > 0)
                    {
                        new AuditDataBLL().InsertAuditData<DeliveryOrderDetailInfo>(null, deliveryOrderDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "DELIVERY_ORDER_DETAIL", this.dataBaseID[0]);
                        deliveryOrderID = deliveryOrderDetailInfo.DeliveryOrderID;
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return deliveryOrderID;
        }

        public string UpdateDetail(object obj)
        {
            string deliveryOrderID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                DeliveryOrderDetailInfo deliveryOrderDetailInfo = obj as DeliveryOrderDetailInfo;
                DeliveryOrderDetailInfo oldDeliveryOrderDetailInfo = deliveryOrderDetailDA.GetDeliveryOrderDetailByID(dataBase, tran, deliveryOrderDetailInfo.DeliveryOrderID, deliveryOrderDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (deliveryOrderDetailDA.UpdateDeliveryOrderDetail(deliveryOrderDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<DeliveryOrderDetailInfo>(oldDeliveryOrderDetailInfo, deliveryOrderDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "DELIVERY_ORDER_DETAIL", this.dataBaseID[0]);
                    deliveryOrderID = deliveryOrderDetailInfo.DeliveryOrderID + "," + deliveryOrderDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return deliveryOrderID;
        }
    }
}

